﻿Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Licenta3.Common.Classes
Imports System.Collections.ObjectModel

Module Helpers
    Public Sub extractCodDenumire(linie As String, ByRef Cod As String, ByRef Denumire As String)
        Cod = linie.Substring(15, linie.IndexOf("""", 15) - 15)
        Denumire = linie.Substring(15 + Cod.Length + 2, linie.IndexOf("<", 15 + Cod.Length + 2) - 15 - Cod.Length - 2)
    End Sub
    Public Sub FillText(identifier As String, ByRef doc As Microsoft.Office.Interop.Word.Document, var As String, Optional Title As Boolean = True)
        If Title Then
            For Each item As Microsoft.Office.Interop.Word.ContentControl In doc.SelectContentControlsByTitle(identifier)
                item.Range.Text = var
            Next
        Else
            For Each item As Microsoft.Office.Interop.Word.ContentControl In doc.SelectContentControlsByTag(identifier)
                item.Range.Text = var
            Next
        End If
    End Sub
    Public Sub FillList(identifier As String, ByRef doc As Microsoft.Office.Interop.Word.Document, index As Integer, Optional Title As Boolean = True)
        If Title Then
            For Each item As Microsoft.Office.Interop.Word.ContentControl In doc.SelectContentControlsByTitle(identifier)
                item.DropdownListEntries(index).Select()
            Next
        Else
            For Each item As Microsoft.Office.Interop.Word.ContentControl In doc.SelectContentControlsByTag(identifier)
                item.DropdownListEntries(index).Select()
            Next
        End If
    End Sub

    Public Sub DeleteCC(identifier As String, ByRef doc As Microsoft.Office.Interop.Word.Document, Optional Title As Boolean = True)
        If Title Then
            For Each item As Microsoft.Office.Interop.Word.ContentControl In doc.SelectContentControlsByTitle(identifier)
                item.Delete()
            Next
        Else
            For Each item As Microsoft.Office.Interop.Word.ContentControl In doc.SelectContentControlsByTag(identifier)
                item.Delete()
            Next
        End If
    End Sub
    Public Sub FormatPolyline(ByRef pl As Polyline)
        'this will:
        'make it clockwise
        'renumber from north-west
        pl.Closed = True
        Dim area As Double = (pl.GetPoint2dAt(pl.NumberOfVertices - 1).X * pl.StartPoint.Y - pl.GetPoint2dAt(pl.NumberOfVertices - 1).Y * pl.StartPoint.X) / 2
        For i = 0 To pl.NumberOfVertices - 2
            area = area + (pl.GetPoint2dAt(i).X * pl.GetPoint2dAt(i + 1).Y - pl.GetPoint2dAt(i).Y * pl.GetPoint2dAt(i + 1).X) / 2
        Next
        If area > 0 Then pl.ReverseCurve()
        'from now on, the polyline is clockwise
        '>0=> anticlocwise
        '<0=> clockwise check this site http://www.mathopenref.com/coordpolygonarea.html

        Dim mostNV As Point3d = pl.StartPoint
        Dim minEpoint As Point3d = pl.StartPoint
        Dim maxNpoint As Point3d = pl.StartPoint
        Dim mostNVi As Integer
        Dim maxNi As Integer
        Dim minEi As Integer
        'Dim mostNE = False
        Dim pt As Point3d
        For i = 0 To pl.NumberOfVertices - 1
            pt = pl.GetPoint3dAt(i)
            If minEpoint.X > pt.X Then
                minEpoint = pt
                minEi = i
            End If
            If maxNpoint.Y < pt.Y Then
                maxNpoint = pt
                maxNi = i
            End If
        Next

        If minEi = maxNi OrElse maxNpoint.X - minEpoint.X = maxNpoint.Y - minEpoint.Y OrElse maxNpoint.X - minEpoint.X > maxNpoint.Y - minEpoint.Y Then
            mostNVi = minEi
            mostNV = minEpoint
        Else
            mostNVi = maxNi
            mostNV = maxNpoint
        End If
        'Dim tempPt As Point2d = pl.GetPoint2dAt(mostNVi)
        Dim tempPl As Polyline = pl.Clone

        Dim dela As Integer
        For i = 0 To pl.NumberOfVertices - 1
            dela = (i + mostNVi) Mod pl.NumberOfVertices
            pl.SetPointAt(i, tempPl.GetPoint2dAt(dela))
        Next
        'pl.SetPointAt(0, tempPt)
        tempPl.Dispose()

        'MsgBox(mostNVi.ToString + "=> X: " + mostNV.X.ToString + " Y: " + mostNV.Y.ToString)
        'pl.AddVertexAt(1, New Point2d(0, 0), Nothing, Nothing, Nothing)
        'pl.RemoveVertexAt(1)
    End Sub
    Public Function offsetPt(pt As Point3d, unit As Decimal, sca As Integer, Optional up As Boolean = True, Optional right As Boolean = True) As Point3d
        Dim x, y, z As Decimal
        z = 0
        If up Then
            y = pt.Y + unit * sca
        Else
            y = pt.Y - unit * sca
        End If
        If right Then
            x = pt.X + unit * sca
        Else
            x = pt.X - unit * sca
        End If
        offsetPt = New Point3d(x, y, z)
    End Function
    Public Sub numberPolyline(pl As Polyline, scA As Double, ByRef acCurDb As Database, ByRef acTrans As Transaction, ByRef acBlkTblRec As BlockTableRecord)
        Dim centr As New Point3d
        centr = centroid(pl)
        For i = 0 To pl.NumberOfVertices - 1
            Dim ptText As New DBText
            Dim up, right As Boolean
            With ptText
                .SetDatabaseDefaults()
                If pl.GetPoint3dAt(i).X <= centr.X Then
                    .HorizontalMode = TextHorizontalMode.TextRight
                    right = False
                Else
                    .HorizontalMode = TextHorizontalMode.TextLeft
                    right = True
                End If
                If pl.GetPoint3dAt(i).Y <= centr.Y Then
                    .VerticalMode = TextVerticalMode.TextTop
                    up = False
                Else
                    .VerticalMode = TextVerticalMode.TextBottom
                    up = True
                End If
                .AlignmentPoint = offsetPt(pl.GetPoint3dAt(i), 1, scA, up, right)
                .TextString = (i + 1).ToString
                .Height = 2 * scA
                .AdjustAlignment(acCurDb)
            End With
            acBlkTblRec.AppendEntity(ptText)
            acTrans.AddNewlyCreatedDBObject(ptText, True)
        Next
    End Sub
    'dimensionari pe fiecare latura
    Public Sub dimPolyline(pl As Polyline, scA As Decimal, ByRef acCurDb As Database, ByRef acTrans As Transaction, ByRef acBlkTblRec As BlockTableRecord)
        Dim offsetPl As New Polyline
        offsetPl = pl.GetOffsetCurves(-2 * scA)(0)
        For i = 0 To pl.NumberOfVertices - 1
            Dim distTxt As New DBText
            With distTxt
                .SetDatabaseDefaults()
                If pl.GetLineSegment2dAt(i).Direction.Angle > Math.PI / 2 And pl.GetLineSegment2dAt(i).Direction.Angle < 3 * Math.PI / 2 Then
                    .Rotation = pl.GetLineSegment2dAt(i).Direction.Angle + Math.PI
                Else
                    .Rotation = pl.GetLineSegment2dAt(i).Direction.Angle
                End If
                .HorizontalMode = TextHorizontalMode.TextCenter
                .VerticalMode = TextVerticalMode.TextVerticalMid
                .AlignmentPoint = New Point3d((offsetPl.GetLineSegment2dAt(i).StartPoint.X + offsetPl.GetLineSegment2dAt(i).EndPoint.X) / 2, _
                                              (offsetPl.GetLineSegment2dAt(i).StartPoint.Y + offsetPl.GetLineSegment2dAt(i).EndPoint.Y) / 2, _
                                              0)
                .TextString = Math.Round(pl.GetLineSegment2dAt(i).Length, 2).ToString & "m"
                .Height = 2 * scA
                .AdjustAlignment(acCurDb)
            End With
            acBlkTblRec.AppendEntity(distTxt)
            acTrans.AddNewlyCreatedDBObject(distTxt, True)
        Next
        offsetPl.Dispose()
    End Sub
    Public Sub veciniPolyline(pl As Polyline, scA As Decimal, vecini As ObservableCollection(Of PersoanaClass), ByRef acCurDb As Database, ByRef acTrans As Transaction, ByRef acBlkTblRec As BlockTableRecord)
        Dim offsetPl As New Polyline
        offsetPl = pl.GetOffsetCurves(-6 * scA)(0)
        For i = 0 To pl.NumberOfVertices - 1
            If vecini(i).Nume IsNot Nothing Then
                Dim txtVecini As New DBText
                With txtVecini
                    .SetDatabaseDefaults()
                    If pl.GetLineSegment2dAt(i).Direction.Angle > Math.PI / 2 And pl.GetLineSegment2dAt(i).Direction.Angle < 3 * Math.PI / 2 Then
                        .Rotation = pl.GetLineSegment2dAt(i).Direction.Angle + Math.PI
                    Else
                        .Rotation = pl.GetLineSegment2dAt(i).Direction.Angle
                    End If
                    .HorizontalMode = TextHorizontalMode.TextCenter
                    .VerticalMode = TextVerticalMode.TextVerticalMid
                    .AlignmentPoint = New Point3d((offsetPl.GetLineSegment2dAt(i).StartPoint.X + offsetPl.GetLineSegment2dAt(i).EndPoint.X) / 2, _
                                                  (offsetPl.GetLineSegment2dAt(i).StartPoint.Y + offsetPl.GetLineSegment2dAt(i).EndPoint.Y) / 2, _
                                                  0)
                    .TextString = vecini(i).Nume
                    .Height = 2.5 * scA
                    .AdjustAlignment(acCurDb)
                End With
                acBlkTblRec.AppendEntity(txtVecini)
                acTrans.AddNewlyCreatedDBObject(txtVecini, True)
            End If
        Next
        offsetPl.Dispose()
    End Sub
    Public Function centroid(pl As Polyline) As Point3d
        centroid = New Point3d((pl.GeometricExtents.MaxPoint.X + pl.GeometricExtents.MinPoint.X) / 2, _
                            (pl.GeometricExtents.MaxPoint.Y + pl.GeometricExtents.MinPoint.Y) / 2, _
                            0)
    End Function
End Module

